﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HYJHouseKeeping.Model;

namespace HYJHouseKeeping.DAL
{
    /// <summary>
    /// 部门Service
    /// </summary>
    public class DeptService
    {
        #region 部门管理
        /// <summary>
        /// 根据部门编号查询部门员工
        /// </summary>
        /// <param name="deptID">部门编号</param>
        /// <returns>部门员工集合</returns>
        public List<Employee> GetEmployeeByDept(int deptID)
        {
            using (HYJHousekeepingEntities hyjhouse = new HYJHousekeepingEntities())
            {
                List<Employee> list = hyjhouse.Employees.Where(e => e.DeptID == deptID).ToList();
                return list;
            }
        }
        /// <summary>
        /// 查看部门信息和部门人数
        /// </summary>
        /// <returns></returns>
        public List<Dept> GetAllDept(out int count)
        {
            using (HYJHousekeepingEntities hyjhouse = new HYJHousekeepingEntities())
            {
                List<Dept> list = hyjhouse.Depts.ToList();
                count = list.Count();
                return list;
            }
        }

        /// <summary>
        /// 根据部门编号修改部门信息 
        /// </summary>
        /// <param name="dept"></param>
        /// <returns>bool</returns>
        public bool UpdateDept(Dept dept, int deptID)
        {
            bool result = false;
            using (HYJHousekeepingEntities hyjhouse = new HYJHousekeepingEntities())
            {
                Dept dept1 = hyjhouse.Depts.SingleOrDefault(d => d.DeptID == deptID);
                if (dept1 != null)
                {
                    dept1.DeptID = dept.DeptID;
                    dept1.DeptName = dept.DeptName;
                }
                if (hyjhouse.SaveChanges() > 0)
                {
                    result = true;
                }
            }
            return result;
        }
        /// <summary>
        /// 新增部门信息
        /// </summary>
        /// <param name="dept"></param>
        /// <returns>bool</returns>
        public bool AddDept(Dept dept)
        {
            bool result = false;
            using (HYJHousekeepingEntities hyjhouse = new HYJHousekeepingEntities())
            {
                hyjhouse.Depts.Add(new Dept()
                                       {
                                           DeptID=dept.DeptID,
                                           DeptName=dept.DeptName
                                       });
                if (hyjhouse.SaveChanges()>0)
                {
                    result = true;
                }
            }
            return result;
        }
        /// <summary>
        /// 删除部门信息
        /// </summary>
        /// <returns>bool</returns>
        public bool DelDept(int deptID)
        {
            bool result = false;
            using (HYJHousekeepingEntities hyjhoouse = new HYJHousekeepingEntities())
            {
                Dept dept = hyjhoouse.Depts.FirstOrDefault(d=>d.DeptID==deptID);
                hyjhoouse.Depts.Remove(dept);
                if (hyjhoouse.SaveChanges()>0)
                {
                    result = true;
                }
            }
            return result;
        }
        #endregion
    }
}
